#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main(void)
{
    int sockfd = socket(PF_INET, SOCK_STREAM, 0); //创建socket
    if (-1 == sockfd) { perror("socket"); exit(1); }

    struct sockaddr_in server_addr;   //保存服务器的信息
    server_addr.sin_family = PF_INET;
    server_addr.sin_port = htons(8000);
    server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");

    int ret = connect(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)); //发起连接
    if (-1 == ret) { perror("connect"); exit(1); }

    printf("连接成功\n");

    char buf[128] = {0};
    ssize_t size;
    while (1) {
        scanf("%s", buf);
        size = send(sockfd, buf, strlen(buf), 0);
        if (-1 == size) { perror("send"); }
        memset(buf, 0, 128);
    }
    return 0;
}
